package thread.thread;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

@Slf4j
public class ThreadRunableAndFutureTaskDemo {

    @Test
    public void test1() {
        Thread hello_world_runnable = new Thread(new Runnable() {
            @Override
            public void run() {
                log.info("hello world runnable");
            }
        });
        hello_world_runnable.start();

        try {
            // synchronized 同步方法
            hello_world_runnable.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // Future 接口提供了 get() 和 isDone()
        FutureTask<String> future = new FutureTask<String>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return "hello world callable";
            }
        });

        Thread thread = new Thread(future);
        thread.start();

        try {
            log.info(future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }


    }

}
